package Java并发编程的艺术.第一章;

public class DeadLock {
    private static String resourceA = "A";
    private static String resourceB = "B";

    public static void deadLock() throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"尝试获取A。。。");
                synchronized (resourceA) {
                    System.out.println(Thread.currentThread().getName()+"获取A成功。。。");
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"尝试获取B。。。");
                    synchronized (resourceB) {
                        System.out.println(Thread.currentThread().getName()+"获取B成功。。。");
                    }
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"尝试获取B。。。");
                synchronized (resourceB) {
                    System.out.println(Thread.currentThread().getName()+"获取B成功。。。");
                    System.out.println(Thread.currentThread().getName()+"尝试获取A。。。");
                    synchronized (resourceA) {
                        System.out.println(Thread.currentThread().getName()+"获取A成功。。。");
                    }
                }
            }
        });

        t1.start();
        t2.start();
    }

    public static void main(String[] args) throws InterruptedException {
        deadLock();
    }
}
